package com.guodu.dao.impl;

import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.PurchaseDistributionPlantzDao;
import com.guodu.entity.FileTechnical;
import com.guodu.entity.FileTechnicaltz;
import com.guodu.entity.PurchaseDistributionPlantz;
import com.guodu.entity.PurchasedistributiontzStatus;
import com.guodu.entity.Units;
import com.guodu.vo.PurchaseDistributionPlantzQueryVo;
import com.guodu.vo.PurchaseDistributionPlantzVo;

@Repository("purchaseDistributionPlantzDao")
public class PurchaseDistributionPlantzDaoImpl extends BaseDao implements PurchaseDistributionPlantzDao {

	@Override
	public long findSizeByVo(PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from PurchaseDistributionPlantz m where 1=1");
		if(pdpq!=null){
			if (pdpq.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdpq.getFunitsId()+" )");
			}
			if (pdpq.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+pdpq.getUnitsId()+")");
			}
			if (pdpq.getTypes()!=null && !"".equals(pdpq.getTypes())) {
				hql.append("    and types like '%"+pdpq.getTypes()+"%'");
			}
			if (pdpq.getProjectName()!=null && !"".equals(pdpq.getProjectName())) {
				hql.append("    and projectName like '%"+pdpq.getProjectName()+"%'");
			}
			if (pdpq.getTexture()!=null && !"".equals(pdpq.getTexture())) {
				hql.append("    and texture like '%"+pdpq.getTexture()+"%'");
			}
			if (pdpq.getDrawingnum()!=null && !"".equals(pdpq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdpq.getDrawingnum()+"%'");
			}
			if (pdpq.getJhbeginDate()!=null && !"".equals(pdpq.getJhbeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+pdpq.getJhbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getJhendDate()!=null && !"".equals(pdpq.getJhendDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+pdpq.getJhendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateBeginTime()!=null && !"".equals(pdpq.getCreateBeginTime())) {
				hql.append("    and createTime >= TO_DATE('"+pdpq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateEndTime()!=null && !"".equals(pdpq.getCreateEndTime())) {
				hql.append("    and createTime <= TO_DATE('"+pdpq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(pdpq.getPurchaseYears()!=null&&pdpq.getPurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') >= TO_DATE('"+pdpq.getPurchaseYears()+"','yyyy')");
			}
			if(pdpq.getEurchaseYears()!=null&&pdpq.getEurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') <= TO_DATE('"+pdpq.getEurchaseYears()+"','yyyy')");
			}
			if(pdpq.getStatus()!=null&&pdpq.getStatus().trim().length()>0&&!pdpq.getStatus().equals("5")){
				hql.append(" and status ="+pdpq.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		return super.findInt(hql.toString());
	}
	@Override
	public void examinAll(PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		if(units==null){
			hql.append("update from PurchaseDistributionPlantz m set m.status=3 where m.status=0 ");			
		}else if(units.getUnitsLevel()==1L){
			hql.append("update from PurchaseDistributionPlantz m set m.status=1 where m.status=0 ");
		}
		if(pdpq!=null){
			if (pdpq.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdpq.getFunitsId()+" )");
			}
			if (pdpq.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+pdpq.getUnitsId()+")");
			}
			if (pdpq.getTypes()!=null && !"".equals(pdpq.getTypes())) {
				hql.append("    and types like '%"+pdpq.getTypes()+"%'");
			}
			if (pdpq.getProjectName()!=null && !"".equals(pdpq.getProjectName())) {
				hql.append("    and projectName like '%"+pdpq.getProjectName()+"%'");
			}
			if (pdpq.getTexture()!=null && !"".equals(pdpq.getTexture())) {
				hql.append("    and texture like '%"+pdpq.getTexture()+"%'");
			}
			if (pdpq.getDrawingnum()!=null && !"".equals(pdpq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdpq.getDrawingnum()+"%'");
			}
			if (pdpq.getJhbeginDate()!=null && !"".equals(pdpq.getJhbeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+pdpq.getJhbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getJhendDate()!=null && !"".equals(pdpq.getJhendDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+pdpq.getJhendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateBeginTime()!=null && !"".equals(pdpq.getCreateBeginTime())) {
				hql.append("    and createTime >= TO_DATE('"+pdpq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateEndTime()!=null && !"".equals(pdpq.getCreateEndTime())) {
				hql.append("    and createTime <= TO_DATE('"+pdpq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(pdpq.getPurchaseYears()!=null&&pdpq.getPurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') >= TO_DATE('"+pdpq.getPurchaseYears()+"','yyyy')");
			}
			if(pdpq.getEurchaseYears()!=null&&pdpq.getEurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') <= TO_DATE('"+pdpq.getEurchaseYears()+"','yyyy')");
			}
			if(pdpq.getStatus()!=null&&pdpq.getStatus().trim().length()>0&&!pdpq.getStatus().equals("5")){
				hql.append(" and status ="+pdpq.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		super.executeUpdate(hql.toString());	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseDistributionPlantz> findByVo(
			PurchaseDistributionPlantzQueryVo pdpq, Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchaseDistributionPlantz m where 1=1");
		if(pdpq!=null){
			if (pdpq.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdpq.getFunitsId()+" )");
			}
			if (pdpq.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+pdpq.getUnitsId()+")");
			}
			if (pdpq.getTypes()!=null && !"".equals(pdpq.getTypes())) {
				hql.append("    and types like '%"+pdpq.getTypes()+"%'");
			}
			if (pdpq.getProjectName()!=null && !"".equals(pdpq.getProjectName())) {
				hql.append("    and projectName like '%"+pdpq.getProjectName()+"%'");
			}
			if (pdpq.getTexture()!=null && !"".equals(pdpq.getTexture())) {
				hql.append("    and texture like '%"+pdpq.getTexture()+"%'");
			}
			if (pdpq.getDrawingnum()!=null && !"".equals(pdpq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdpq.getDrawingnum()+"%'");
			}
			if (pdpq.getJhbeginDate()!=null && !"".equals(pdpq.getJhbeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+pdpq.getJhbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getJhendDate()!=null && !"".equals(pdpq.getJhendDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+pdpq.getJhendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateBeginTime()!=null && !"".equals(pdpq.getCreateBeginTime())) {
				hql.append("    and createTime >= TO_DATE('"+pdpq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateEndTime()!=null && !"".equals(pdpq.getCreateEndTime())) {
				hql.append("    and createTime <= TO_DATE('"+pdpq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(pdpq.getPurchaseYears()!=null&&pdpq.getPurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') >= TO_DATE('"+pdpq.getPurchaseYears()+"','yyyy')");
			}
			if(pdpq.getEurchaseYears()!=null&&pdpq.getEurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') <= TO_DATE('"+pdpq.getEurchaseYears()+"','yyyy')");
			}
			if(pdpq.getStatus()!=null&&pdpq.getStatus().trim().length()>0&&!pdpq.getStatus().equals("5")){
				hql.append(" and status ="+pdpq.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		hql.append(" order by purchaseId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public void save(PurchaseDistributionPlantz purchaseDistributionPlantz) {
		// TODO Auto-generated method stub
		super.save(purchaseDistributionPlantz);
	}

	@Override
	public PurchaseDistributionPlantz findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (PurchaseDistributionPlantz) super.findById(PurchaseDistributionPlantz.class, purchaseId);
	}

	@Override
	public void update(PurchaseDistributionPlantz purchaseDistributionPlantz) {
		// TODO Auto-generated method stub
		super.update(purchaseDistributionPlantz);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasedistributiontzStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return super.findByProperty(PurchasedistributiontzStatus.class, "purchaseId", purchaseId);
	}

	@Override
	public void changeStatus(List<PurchasedistributiontzStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update PurchaseDistributionPlantz set status =");
		hql.append(status);
		hql.append(" where purchaseId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(PurchaseDistributionPlantz.class, delIds);
	}

	@Override
	public void save(FileTechnicaltz fileTechnicaltz) {
		// TODO Auto-generated method stub
		super.save(fileTechnicaltz);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FileTechnicaltz> findFile(String technical) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer(" from FileTechnicaltz f where f.purchaseId = '"+technical+"'");
		return (List<FileTechnicaltz>) super.find(hql.toString());
	}

	@Override
	public void delZi(String technical) {
		// TODO Auto-generated method stub
		List<FileTechnicaltz> fileTechnical = this.findFile(technical);
		for (FileTechnicaltz fileTechnical2 : fileTechnical) {
			String path = ServletActionContext.getServletContext().getRealPath("/")+ "/upload/PurchaseDistributionPlantz/";
			File file = new File(path+fileTechnical2.getFileRealname());
			if (file.isFile() && file.exists()) { 
				file.delete();
			}
			delete(FileTechnical.class, fileTechnical2.getFileTechnicalId());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchaseDistributionPlantz> findAllByVo(
			PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchaseDistributionPlantz m where 1=1");
		if(pdpq!=null){
			if (pdpq.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdpq.getFunitsId()+" )");
			}
			if (pdpq.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+pdpq.getUnitsId()+")");
			}
			if (pdpq.getTypes()!=null && !"".equals(pdpq.getTypes())) {
				hql.append("    and types like '%"+pdpq.getTypes()+"%'");
			}
			if (pdpq.getProjectName()!=null && !"".equals(pdpq.getProjectName())) {
				hql.append("    and projectName like '%"+pdpq.getProjectName()+"%'");
			}
			if (pdpq.getTexture()!=null && !"".equals(pdpq.getTexture())) {
				hql.append("    and texture like '%"+pdpq.getTexture()+"%'");
			}
			if (pdpq.getDrawingnum()!=null && !"".equals(pdpq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdpq.getDrawingnum()+"%'");
			}
			if (pdpq.getJhbeginDate()!=null && !"".equals(pdpq.getJhbeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+pdpq.getJhbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getJhendDate()!=null && !"".equals(pdpq.getJhendDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+pdpq.getJhendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateBeginTime()!=null && !"".equals(pdpq.getCreateBeginTime())) {
				hql.append("    and createTime >= TO_DATE('"+pdpq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateEndTime()!=null && !"".equals(pdpq.getCreateEndTime())) {
				hql.append("    and createTime <= TO_DATE('"+pdpq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(pdpq.getPurchaseYears()!=null&&pdpq.getPurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') >= TO_DATE('"+pdpq.getPurchaseYears()+"','yyyy')");
			}
			if(pdpq.getEurchaseYears()!=null&&pdpq.getEurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') <= TO_DATE('"+pdpq.getEurchaseYears()+"','yyyy')");
			}
			if(pdpq.getStatus()!=null&&pdpq.getStatus().trim().length()>0&&!pdpq.getStatus().equals("5")){
				hql.append(" and status ="+pdpq.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}

	
	public PurchaseDistributionPlantzVo findAllMoney(
			PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		StringBuffer hql = new StringBuffer("select sum(estimatedAmount),sum(1) from PurchaseDistributionPlantz m where 1=1");
		if(pdpq!=null){
			if (pdpq.getFunitsId()!=null) {
				hql.append("    and exists (select 1 from Units u where u.unitsId = m.unitsId and u.funitsId  ="+pdpq.getFunitsId()+" )");
			}
			if (pdpq.getUnitsId()!=null) {
				hql.append("    and exists ( select 1 from UserInfo u where u.userId=m.createPeople and u.unitsId="+pdpq.getUnitsId()+")");
			}
			if (pdpq.getTypes()!=null && !"".equals(pdpq.getTypes())) {
				hql.append("    and types like '%"+pdpq.getTypes()+"%'");
			}
			if (pdpq.getProjectName()!=null && !"".equals(pdpq.getProjectName())) {
				hql.append("    and projectName like '%"+pdpq.getProjectName()+"%'");
			}
			if (pdpq.getTexture()!=null && !"".equals(pdpq.getTexture())) {
				hql.append("    and texture like '%"+pdpq.getTexture()+"%'");
			}
			if (pdpq.getDrawingnum()!=null && !"".equals(pdpq.getDrawingnum())) {
				hql.append("    and drawingnum like '%"+pdpq.getDrawingnum()+"%'");
			}
			if (pdpq.getJhbeginDate()!=null && !"".equals(pdpq.getJhbeginDate())) {
				hql.append("    and arrivalTime >= TO_DATE('"+pdpq.getJhbeginDate()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getJhendDate()!=null && !"".equals(pdpq.getJhendDate())) {
				hql.append("    and arrivalTime <= TO_DATE('"+pdpq.getJhendDate()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateBeginTime()!=null && !"".equals(pdpq.getCreateBeginTime())) {
				hql.append("    and createTime >= TO_DATE('"+pdpq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (pdpq.getCreateEndTime()!=null && !"".equals(pdpq.getCreateEndTime())) {
				hql.append("    and createTime <= TO_DATE('"+pdpq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(pdpq.getPurchaseYears()!=null&&pdpq.getPurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') >= TO_DATE('"+pdpq.getPurchaseYears()+"','yyyy')");
			}
			if(pdpq.getEurchaseYears()!=null&&pdpq.getEurchaseYears().trim().length()>0){
				hql.append(" and TO_DATE(purchaseYears,'yyyy') <= TO_DATE('"+pdpq.getEurchaseYears()+"','yyyy')");
			}
			if(pdpq.getStatus()!=null&&pdpq.getStatus().trim().length()>0&&!pdpq.getStatus().equals("5")){
				hql.append(" and status ="+pdpq.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==1){
			hql.append("and status=1");
		}else if(units.getUnitsLevel()==1L&&units.getIsComplete()==0){
			hql.append("and (status=0 or status=1 or status=2 or status=3 or status=4)");
		}
		PurchaseDistributionPlantzVo p = new PurchaseDistributionPlantzVo();
		List<Object[]> objs = super.find(hql.toString());
		if(objs.size()>0){
			DecimalFormat df = new DecimalFormat("##,###.##");
			try {
				if(objs.get(0)[0]!=null){
				p.setAllMoney1(df.format(df.parse(objs.get(0)[0].toString())));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		return p;
	}

	

}
